<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/7
 * Time: 15:40
 */

namespace app\controllers;

use yii;
use app\models\Quote;
use app\models\QuoteProduct;

class QuoteController extends ApiBaseController
{
    public function actionAdd()
    {
        $data = yii::$app->request->post('data');
        if (!$data) {
            return \json_encode(['code' => 0, 'msg' => '参数不能为空']);
        }
        try {
            $msg = \json_decode($data, true);
            $data = [];
            $data['list'] = $msg['list'];
            $data['customer_info'] = $msg['customer_info'];
            $data['para'] = \json_encode($msg['para']);//段落
            $data['duty'] = \json_encode($msg['duty']);//税费
            $data['ship'] = \json_encode($msg['ship']);//运费
            $data['service'] = \json_encode($msg['service']);//运费
            //生成一个报价编号
            $model = new \app\models\Quote();
            $re = $model->get_quote_id();
            $quote_num = $re ? ($re['num'] + 1) : 1;
            $data['quote_id'] = 'Q' . date('ymd') . str_pad($quote_num, 3, 0, STR_PAD_LEFT);

            $list = $data['list'];
            $arr = [];
            //添加到采购列表
            $quoteProductModel = new QuoteProduct();
            foreach ($list as $k => $v) {
                $v['subtotal'] = $v['price'] * $v['pcs'] + $v['extra_fee'];
                if ($v['item']) {
                    $tmp = $v;
                    $tmp['product_name'] = $v['item'];
                    $tmp['job_number'] = $this->user['job_number'];
                    $tmp['date'] = date('Y-m-d');
                    $tmp['img_src'] = !empty($v['img_src']) ? $v['img_src'][0] : '';
                    unset($tmp['item']);
                    unset($tmp['extra_fee']);
                    unset($tmp['subtotal']);
                    $quoteProductModel->insert($tmp);
                    $arr[] = $v;
                }
            }
            //数据录入数据库`
            $user = $this->user;
            $insert_data = [
                'date' => date('Y-m-d'),
                'job_number' => $user['job_number'],
                'sale_rep' => $user['name'],
                'customer_info' => json_encode($data['customer_info']),
                'product_list' => json_encode($arr),
                'number' => $data['customer_info']['number'],
                'quote_id' => $data['quote_id'],
                'para' => $data['para'],
                'ship' => $data['ship'],
                'duty' => $data['duty'],
                'service' => $data['service'],
                'extra_fee_show' => true,
                'total_amount_show' => true,
                'subtotal_show' => true,
                'create_at' => time()
            ];
            $model = new Quote();
            $re = $model->add_quote($insert_data);
            if (!$re) {
                echo json_encode(['code' => 0, 'msg' => '添加失败,请重试']);
            } else {
                echo json_encode(['code' => 1, 'msg' => '添加成功']);
            }
        } catch (\Exception $e) {
            return \json_encode(['code' => 0, 'msg' => ($e->getMessage())]);
        }

    }

    public function actionInfo()
    {
        $id = yii::$app->request->get('id');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, 'msg' => '参数不对']);
        }
        $model = new Quote();
        $re = $model->one('*', ['id' => $id]);
        return \json_encode($re);
    }

    public function actionUpdate()
    {
        $id = yii::$app->request->post('id');
        $info = yii::$app->request->post('data');
        if (!is_numeric($id) || $id < 0) {
            return \json_encode(['code' => 0, "msg" => "参数错误"]);
        }
        //复制一条记录
        $model = new \app\models\Quote();
        $data = $model->get_quote_info_by_id($id);
        $msg = \json_decode($info, true);
        $data['customer_info'] = \json_encode($msg['customer_info']);
        $data['para'] = \json_encode($msg['para']);//段落
        $data['duty'] = \json_encode($msg['duty']);//税费
        $data['ship'] = \json_encode($msg['ship']);//运费
        $data['service'] = \json_encode($msg['service']);//运费
        $data['product_list'] = \json_encode($msg['list']);
        $data['create_at'] = time();

        //获取当前quote最大版本;
        $version = $model->get_max_version_by_quote_num($data['quote_id']);
        $data['version'] = $version['version'] + 1;
        unset($data['id']);
        $re = yii::$app->db
            ->createCommand()
            ->insert(yii::$app->db->tablePrefix . "quote", $data)
            ->execute();

        if ($re) {
            echo json_encode(['code' => 1, 'msg' => '编辑成功']);
        } else {
            echo json_encode(['code' => 0, 'msg' => '编辑失败，请重试']);
        }
    }
}